/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.enrstu.repository;

import me.zhengjie.modules.enrstu.domain.AdministrativeDivision;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
* @website https://eladmin.vip
* @author yyx
* @date 2024-10-22
**/
public interface AdministrativeDivisionRepository extends JpaRepository<AdministrativeDivision, String>, JpaSpecificationExecutor<AdministrativeDivision> {
    /**
    * 根据 division_id,divisionLevel 查询
    * @param divisionId /
    * @param divisionLevel /
    * @return /
    */
    AdministrativeDivision findByAdministrativeDivisionIdAndAdministrativeDivisionLevel(Long divisionId, Integer divisionLevel);
    /**
     * 根据 divisionName 查询
     * @param divisionName /
     * @return /
     */
    AdministrativeDivision findByAdministrativeDivisionName(String divisionName);

    /**
     * 根据 divisionName 查询 List
     * @param divisionName /
     * @return /
     */
    @Query("FROM AdministrativeDivision a WHERE " +
            "a.administrativeDivisionName =?1")
    List<AdministrativeDivision> findByAdministrativeDivisionNameList(String divisionName);
    /**
     * 根据 divisionLevel 查询
     * @param divisionLevel /
     * @return /
     */
    List<AdministrativeDivision> findByAdministrativeDivisionLevel(Integer divisionLevel);
    /**
     * 根据 divisionSign，divisionLevel 查询
     * @param divisionSign /
     * @param divisionLevel /
     * @return /
     */
    List<AdministrativeDivision> findByAdministrativeDivisionSignAndAdministrativeDivisionLevel(Integer divisionSign, Integer divisionLevel);
    /**
     * 根据 divisionId 查询
     * @param divisionId /
     * @return /
     */
    AdministrativeDivision findByAdministrativeDivisionId(Long divisionId);



    @Query("FROM AdministrativeDivision AS a JOIN AdministrativeDivision b ON a.administrativeDivisionParentId = b.administrativeDivisionId WHERE " +
            "a.administrativeDivisionParentId = ?1 AND a.administrativeDivisionSign =?2")
    List<AdministrativeDivision>  findByIdSubLevelList(@Param("administrativeDivisionParentId") Long administrativeDivisionParentId,@Param("administrativeDivisionSign") Integer administrativeDivisionSign);
    Boolean existsByAdministrativeDivisionLevelAndAdministrativeDivisionName(Integer administrativeDivisionLevel,String administrativeDivisionName);

    /**
     *
     */
    @Query("FROM AdministrativeDivision AS a  WHERE a.administrativeDivisionSign = ?1 AND a.administrativeDivisionLevel =?2")
    AdministrativeDivision findBySignAndLevel(Integer sign, Integer level);



}